home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Incognito / cdev / CListManager.cp < prev    next >
Encoding:
Text File  |  1994-02-09  |  8.1 KB  |  465 lines  |  [TEXT/MPS ]

  1. #ifndef __CLISTMANAGER__
  2. #include "CListManager.h"
  3. #endif
  4.  
  5. #ifndef __TEXTUTILS__
  6. #include <TextUtils.h>
  7. #endif
  8.  
  9. #ifndef __DIALOGUTIL__
  10. #include <DialogUtil.h>
  11. #endif
  12.  
  13. #ifndef __FONTS__
  14. #include <Fonts.h>
  15. #endif
  16.  
  17. #ifndef __STRING__
  18. #include <String.h>
  19. #endif
  20.  
  21. /*
  22.   MyCompareProc does a straight memcompare, because who really gives a damn if the
  23.   script isn't english?
  24. */
  25.  
  26. static pascal Boolean MyCompareProc(const void* a, const void* b, short aLen, short bLen)
  27. {
  28.     if (!aLen || !bLen) return true;
  29.     return !memcmp(a, b, aLen > bLen ? aLen : bLen);
  30. //    return !EqualString((ConstStr255Param) a, (ConstStr255Param) b, false, true);
  31. }
  32.  
  33. void CListManager::RemoveItem(StringPtr theItem)
  34. {
  35.     Cell    theCell;
  36.     
  37.     if (FindItem(theItem, theCell))
  38.     {
  39.         RemoveItem(theCell);
  40.     }
  41. }
  42.  
  43. Boolean CListManager::IsItemSelected(StringPtr theItem)
  44. {
  45.     Cell    theCell;
  46.     
  47.     if (FindItem(theItem, theCell))
  48.     {
  49.         return IsItemSelected(theCell);
  50.     }
  51.     return false;
  52. }
  53.  
  54. /*
  55.   Inserts an item into the list in sorted order. How about that?
  56. */
  57.  
  58. Boolean CListManager::InsertItem(StringPtr theItem)
  59. {
  60.     Boolean    good;
  61.     Cell    theCell = {0,0};
  62.     
  63.     if (good = BinarySearch(theItem, theCell))
  64.     {
  65.         AddItem(theItem, theCell);
  66.     }
  67.     return good;
  68. }
  69.  
  70. /*
  71.   inserts a row into the list, and sets the row to the data passed
  72.   in.
  73. */
  74.  
  75. void CListManager::AddItem(StringPtr name, Cell theCell)
  76. {
  77.     short length = name[0];
  78.  
  79.     LAddRow(1, theCell.v, fTheList);
  80.     LSetCell(name + 1, length, theCell, fTheList);
  81. }
  82.  
  83. /*
  84.   retrieves an item from the list.
  85. */
  86.  
  87. Boolean CListManager::GetItem(StringPtr theData, Cell &theCell)
  88. {
  89.     short length = 31;
  90.     
  91.     if (GetHeight() && GetWidth() && (theCell.v >= 0) && (theCell.v < GetHeight()) && (theCell.h >= 0) && (theCell.h < GetWidth()))
  92.     {
  93.         LGetCell(theData + 1, &length, theCell, fTheList);
  94.         theData[0] = length;
  95.         return true;
  96.     }
  97.     else return false;
  98. }
  99.  
  100. /*
  101.   Removes an item from the list. Destroys the row, basically.
  102. */
  103.  
  104. void CListManager::RemoveItem(Cell theCell)
  105. {
  106.     LDelRow(1, theCell.v, fTheList);
  107. }
  108.  
  109. /*
  110.   Finds theData if it is in the list. Returns true if the item is
  111.   present, and sets theCell to its cell.
  112. */
  113.  
  114. Boolean CListManager::FindItem(StringPtr theData, Cell& theCell)
  115. {
  116.     return !BinarySearch(theData, theCell);
  117. }
  118.  
  119.  
  120. /*
  121.   Is there a selection in the list? If so, it returns the first selected
  122.   item. Destroys the contents of theCell.
  123. */
  124.  
  125. Boolean CListManager::IsSelection(Cell& theCell)
  126. {
  127.     theCell.h = theCell.v = 0;
  128.     return LGetSelect(true, &theCell, fTheList);
  129. }
  130.  
  131. /*
  132.   is the specified cell selected? returns true if it is.
  133. */
  134.  
  135. Boolean CListManager::IsItemSelected(Cell& theCell)
  136. {
  137.     return LGetSelect(false, &theCell, fTheList);
  138. }
  139.  
  140. /*
  141.   Finds the next selected item. Returns true if there is one, setting
  142.   theCell to the item.
  143. */
  144.  
  145. Boolean CListManager::FindNextSelection(Cell& theCell)
  146. {
  147.     return LGetSelect(true, &theCell, fTheList);
  148. }
  149.  
  150. /*
  151.     Deselects a list
  152. */
  153.  
  154. void CListManager::DeselectList()
  155. {
  156.     Cell    theCell = {0, 0};
  157.     
  158.     while (LGetSelect(true, &theCell, fTheList))
  159.     {
  160.         LSetSelect(false, theCell, fTheList);
  161.     }
  162. }
  163.  
  164. void CListManager::SelectItem(StringPtr theItem)
  165. {
  166.     Cell    theCell;
  167.     
  168.     if (FindItem(theItem, theCell))
  169.     {
  170.         SelectItem(theCell);
  171.     }
  172. }
  173.  
  174. Boolean CListManager::GetCurrentSelection(StringPtr theItem)
  175. {
  176.     Cell    theCell;
  177.     
  178.     if (IsSelection(theCell))
  179.     {
  180.         return GetItem(theItem, theCell);
  181.     }
  182.     return false;
  183. }
  184.  
  185. #pragma segment Main
  186. CListManager::CListManager(short dialogItem, DialogPtr theDialog, short width, short height)
  187. {
  188.     Rect        dataBounds, rView;
  189.     Point        cellSize;
  190.     GrafPtr        oldPort;
  191.     FontInfo    theInfo;
  192.     
  193.     GetPort(&oldPort);
  194.     SetPort(theDialog);
  195.     
  196.     TextFont(geneva);
  197.     TextSize(9);
  198.     GetFontInfo(&theInfo);
  199.     SetRect(&dataBounds, 0, 0, width, height);
  200.     getBox(theDialog, dialogItem, &rView);
  201.     rView.right -= 15;
  202. //    cellSize.h = width.v = 0;
  203.     cellSize.h = rView.right - rView.left;
  204.     cellSize.h /= width;
  205.     cellSize.v = theInfo.ascent + theInfo.descent + theInfo.leading;
  206.     
  207.     fTheList = LNew(&rView, &dataBounds, cellSize, 0, theDialog, true, false, false, true);
  208.     if (fTheList)
  209.     {
  210.         (**fTheList).selFlags = lUseSense + lNoRect + lNoExtend;
  211.         (**fTheList).listFlags = 2;
  212.         LSetDrawingMode(true, fTheList);
  213.     }
  214.     SetPort(oldPort);
  215. }
  216.  
  217.  
  218. #pragma segment Main
  219. CListManager::~CListManager()
  220. {
  221.     LDispose(fTheList);
  222. }
  223.  
  224.  
  225. #pragma segment Main
  226. void    CListManager::Activate()
  227. {
  228.     LActivate(true, fTheList);
  229. }
  230.  
  231.  
  232. #pragma segment Main
  233. void    CListManager::Deactivate()
  234. {
  235.     LActivate(false, fTheList);
  236. }
  237.  
  238.  
  239. #pragma segment Main
  240. void    CListManager::Update()
  241. {
  242.     LUpdate((*fTheList)->port->visRgn, fTheList);
  243. }
  244.  
  245.  
  246. #pragma segment Main
  247. Boolean    CListManager::Click(Point thePoint, short modifiers)
  248. {
  249.     Point    localPoint = thePoint;
  250.     
  251.     GlobalToLocal(&localPoint);
  252.     return LClick(localPoint, modifiers, fTheList);
  253. }
  254.  
  255.  
  256. #pragma segment Main
  257. Boolean CListManager::IsSelection()
  258. {
  259.     Cell    theCell = {0,0};
  260.     
  261.     return LGetSelect(true, &theCell, fTheList);
  262. }
  263.  
  264. #pragma segment Main
  265. short  CListManager::GetHeight()
  266. {
  267.     return (**fTheList).dataBounds.bottom;
  268. }
  269.  
  270.  
  271. #pragma segment Main
  272. short  CListManager::GetWidth()
  273. {
  274.     return (**fTheList).dataBounds.right;
  275. }
  276.  
  277. #pragma segment Main
  278.  
  279.  
  280.  
  281. #pragma segment Main
  282. Boolean  CListManager::GetNextItem(StringPtr theItem, Cell &theCell)
  283. {
  284.     if ((theCell.h+1) < GetWidth()) theCell.h++;
  285.     else
  286.     {
  287.         theCell.h = 0;
  288.         theCell.v++;
  289.     }
  290.     return GetItem(theItem, theCell);
  291. }
  292.  
  293.  
  294.  
  295. #pragma segment Main
  296. void CListManager::AutoScroll()
  297. {
  298.     LAutoScroll(fTheList);
  299. }
  300.  
  301.  
  302. #pragma segment Main
  303. void CListManager::SelectItem(Cell &theCell)
  304. {
  305.     DeselectList();
  306.     LSetSelect(true, theCell, fTheList);
  307.     AutoScroll();
  308. }
  309.  
  310.  
  311. #pragma segment Main
  312. void  CListManager::Empty()
  313. {
  314.     LDelRow(0, 0, fTheList);
  315. }
  316.  
  317. #pragma segment Main
  318. Boolean CListManager::GetFirstItem(StringPtr theItem, Cell &theCell)
  319. {
  320.     theCell.h = 0;
  321.     theCell.v = 0;
  322.     return GetItem(theItem, theCell);
  323. }
  324.  
  325. #pragma segment Main
  326. void CListManager::DoDraw(Boolean drawStuff)
  327. {
  328.     LSetDrawingMode(drawStuff, fTheList);
  329. }
  330.  
  331.  
  332. #pragma segment Main
  333. void CListManager::DeleteSelection(void)
  334. {
  335.     Cell    theCell = {0,0};
  336.     
  337.     while (IsSelection(theCell))
  338.     {
  339.         RemoveItem(theCell);
  340.     }
  341. }
  342.  
  343.  
  344. #pragma segment Main
  345.  
  346.  
  347. #pragma segment Main
  348. Boolean CListManager::IsItemPresent(StringPtr theItem)
  349. {
  350.     Cell    theCell = {0,0};
  351.     
  352.     return FindItem(theItem, theCell);
  353. }
  354.  
  355.  
  356. #pragma segment Main
  357. short CListManager::CompareInfo(Cell theCell, short offset, short length, StringPtr theData)
  358. {
  359.     short    result;
  360.     char    *theCellData;
  361.     
  362.     HLock((*fTheList)->cells);
  363.     theCellData = (*(*fTheList)->cells)+offset;
  364.     result = IUMagString(theData+1, theCellData, theData[0], length);
  365.     HUnlock((*fTheList)->cells);
  366.     return result;
  367. }
  368.  
  369.  
  370. #pragma segment Main
  371. Boolean CListManager::BinarySearch(StringPtr theItem, Cell &theCell)
  372. {
  373.     short middle, top = (**fTheList).dataBounds.top, bottom = (**fTheList).dataBounds.bottom;
  374.     short result;
  375.     short offset, length;        // 4 lfind
  376.     
  377.     theCell.h = theCell.v = 0;
  378.     middle = (bottom - top) / 2;
  379.     while (bottom != middle)
  380.     {
  381.         theCell.v = middle;
  382.         LGetCellDataLocation(&offset, &length, theCell, fTheList);
  383.         if (length == -1) return false;
  384.         
  385.         result = CompareInfo(theCell, offset, length, theItem);
  386.         if (result < 0)
  387.         {
  388.             bottom = middle;
  389.             middle -= ((bottom - top) + 1) / 2;
  390.         }
  391.         else if (result > 0)
  392.         {
  393.             top = middle;
  394.             middle += ((bottom - top) + 1) / 2;
  395.         }
  396.         else
  397.         {
  398.             theCell.v = middle;
  399.             return false;                            // no duplicates!
  400.         }
  401.     }
  402.     theCell.v = bottom;
  403.     return true;
  404. }
  405.  
  406. #pragma segment Main
  407. void CListManager::DeselectItem(Cell &theCell)
  408. {
  409.     LSetSelect(false, theCell, fTheList);
  410. }
  411.  
  412. #pragma segment Main
  413. Boolean CListManager::BinarySearchInsensitive(StringPtr theData, Cell &theCell)
  414. {
  415.     short    middle, top = (**fTheList).dataBounds.top, bottom = (**fTheList).dataBounds.bottom;
  416.     short    result;
  417.     Str32    theItem;
  418.     
  419.     theCell.h = theCell.v = 0;
  420.     middle = (bottom - top) / 2;
  421.     while (bottom != middle)
  422.     {
  423.         theCell.v = middle;
  424.         GetItem(theItem, theCell);
  425.         result = RelString(theData, theItem, false, true);
  426.         if (result < 0)
  427.         {
  428.             bottom = middle;
  429.             middle -= ((bottom - top) + 1) / 2;
  430.         }
  431.         else if (result > 0)
  432.         {
  433.             top = middle;
  434.             middle += ((bottom - top) + 1) / 2;
  435.         }
  436.         else
  437.         {
  438.             theCell.v = middle;
  439.             return false;                            // no duplicates!
  440.         }
  441.     }
  442.     theCell.v = bottom;
  443.     return true;
  444. }
  445.  
  446. #pragma segment Main
  447. void CListManager::SetItem(StringPtr theString, Cell theCell)
  448. {
  449.     short    length = 0;
  450.     
  451.     length = theString[0];
  452.     LSetCell(theString+1, length, theCell, fTheList);
  453. }
  454.  
  455.  
  456.  
  457.  
  458.  
  459. #pragma segment Main
  460.  
  461.  
  462.  
  463.  
  464.  
  465.